<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Regisiter extends MY_Controller {

	//构造函数
	public function __construct()
	{
		parent::__construct();
		$this->load->model('dxdb_model','reg','regisiter');
	}

	public function index()
	{
        $keys = $this->input->get();
        $sqlWhere = " ";  
        if(! empty($keys['sname']) && ! empty($keys['snum'])) $sqlWhere  .= "WHERE `re_name`='".$keys['sname']."' AND `re_number`='".$keys['snum']."'";
        if(! empty($keys['sname']) &&  empty($keys['snum'])) $sqlWhere  .= "WHERE `re_name`='".$keys['sname']."'";
        if(empty($keys['sname']) &&  !empty($keys['snum'])) $sqlWhere  .= "WHERE `re_number`='".$keys['snum']."'";

		$sql = "SELECT DISTINCT(re_name),re_number  FROM `caup_regisiter`".$sqlWhere;
		$num = $this->db->query($sql)->result_array();


        $this->config->set_item('url_suffix', '');
        // 载入分页类
        $this->load->library('pagination');
        $perPage = 20;
        //配置项设置
        $config['base_url'] = site_url('regisiter/index');
        $config['total_rows'] = count($num);
        $config['per_page'] = $perPage;
        $config['uri_segment'] = 3;
        // $config['enable_query_strings'] = TRUE;
        $config['first_link'] = '第一页';
        $config['prev_link'] = '上一页';
        $config['next_link'] = '下一页';
        $config['last_link'] = '最后一页';

            //初始化配置
        $this->pagination->initialize($config);
        $data['links'] = $this->pagination->create_links();
        $offset = $this->uri->segment(3);
        // $this->db->limit($perPage, $offset);
        $sql = "SELECT DISTINCT(re_name),re_number  FROM `caup_regisiter`".$sqlWhere." LIMIT ".(int)$offset.",".$perPage;
        $data['regis'] = $this->db->query($sql)->result_array();
		//  $data['regis']= $this->reg->all();
		$this->load->view('message/reg_list',$data);
	}

	public function reg_add()
	{
		if(IS_POST)
		{
			$reg_data = $this->input->post();
			$flag = $this->reg->dx_insert($reg_data);
			if($flag) $this->success('regisiter/index');
		}
		else
		{
			$this->load->view('message/reg_add');
		}
	}

	public function reg_edit()
	{
    	$re_number = $this->uri->segment(3);
    	if(IS_POST)
    	{
    		$reg_data = $this->input->post();
    		$flag = $this->reg->dx_update($reg_data,array('re_number'=>$re_number));
    		if($flag) $this->success('regisiter/index');
    	}
    	else
    	{
    		$data['one'] = $this->reg->one(array('re_number'=>$re_number));
    		$data['news'] = $this->reg->all(array('re_number'=>$re_number));
    		if(empty($data['one']))
    		{
    			redirect('regisiter/index');
    		}
    		$this->load->view('message/reg_edit',$data);
    	}
	}

    //编辑课号，如果有错误的时候
    public function regisiter_check()
    {
    	$re_number = $this->uri->segment(3);
    	if(IS_POST)
    	{
    		$reg_data = $this->input->post();
    		$flag = $this->reg->dx_update($reg_data,array('re_id'=>$re_number));
    		$re_number = $this->input->post('re_number');
    		if($flag) $this->success('regisiter/reg_edit/'.$re_number);
    	}
    	else
    	{
    		$data['one'] = $this->reg->one(array('re_id'=>$re_number));
    		$this->load->view('message/regisiter_check',$data);
    	}
    }


	public function reg_del()
	{
		$id = intval($this->input->post('id'));
		//删除图片
		$flag = $this->reg->dx_delete(array('re_id'=>$id));
		if($flag)
		{
		  $arr['status']  = 1; $arr['message']  = "删除信息成功 :)";
		}
		else
		{
		   $arr['status']  = 0; $arr['message']  = "操作失败 :(";         
		} 
		$this->ajax($arr); 
	}

	public function downloads()
    {
    	$table = $this->uri->segment(3);
		$this->load->library('table');
		$this->load->library('excel');
		$sql = $this->db->get($table);
		$sql->result();
		$this->excel->filename = time().'_'.$table;
		$this->excel->make_from_db($sql);
	}


	public function evaluation()
	{
		$this->load->model('dxdb_model','eva','course_eva');
        $this->config->set_item('url_suffix', '');
        // 载入分页类
        $this->load->library('pagination');
        $perPage = 18;
        //配置项设置
        $config['base_url'] = site_url('regisiter/evaluation');
        $config['total_rows'] = $this->db->count_all_results('course_eva');
        $config['per_page'] = $perPage;
        $config['uri_segment'] = 3;
        // $config['enable_query_strings'] = TRUE;
        $config['first_link'] = '第一页';
        $config['prev_link'] = '上一页';
        $config['next_link'] = '下一页';
        $config['last_link'] = '最后一页';

            //初始化配置
        $this->pagination->initialize($config);
        $data['links'] = $this->pagination->create_links();
        $offset = $this->uri->segment(3);
        $this->db->limit($perPage, $offset);
		$data['regis'] = $this->eva->all();
		$this->load->view('message/eva_list',$data);
	}

}